# -*- coding: utf-8 -*-
# @Time        :2025/2/28 16:17
# @Author      :文刀水寿
# @File        : 7_爬虫_urllib_post请求百度翻译.py
"""
 @Description :
"""
import urllib.request
import urllib.parse

url = 'https://fanyi.baidu.com/sug'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0'
}

data = {
    'kw': 'final'
}

# post请求的参数，必须要进行编码
data = urllib.parse.urlencode(data).encode('utf-8')

# post的请求的参数，是不会拼接在url后面的 而是放在请求对象定制的参数中
# post请求的参数，必须编码
request = urllib.request.Request(url=url, data=data, headers=headers)

# 模拟向服务器发送请求
response = urllib.request.urlopen(request)

content = response.read().decode('utf8')

import json

obj = json.loads(content)
print(obj)
